﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using Fundway.Common.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public partial class Desk_Bll
    {
        /// <summary>
        /// 获取所有餐桌列表
        /// </summary>
        /// <param name="status"></param>
        /// <returns>list</returns>
        public static List<Desk> GetAllList()
        {
            List<Desk> list = new List<Desk>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.Desks.ToList();
            }
            return list;
        }
        public static Dictionary<int, string> GetDicDeskNumber()
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic = GetAllList().ToDictionary(k => k.ID, v => v.DeskNumber);
            return dic;
        }

        /// <summary>
        /// 获取所有餐桌列表，分页
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="curpage">当前页</param>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="recordCount">总数</param>
        /// <returns></returns>
        public static List<Desk> GetList(string deskNumber, ref PageInfo pageInfo)
        {
            List<Desk> list = new List<Desk>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(deskNumber))
                {
                    list = dbContext.Desks.OrderByDescending(m => m.ID).ToList();
                }
                else
                {
                    list = dbContext.Desks.Where(m => m.DeskNumber.Contains(deskNumber)).OrderByDescending(m => m.ID).ToList();
                }
                pageInfo.TotalCount = list.Count;
                list = list.Skip((pageInfo.CurPage - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList();
            }
            return list;
        }

        /// <summary>
        /// 根据ID查询餐桌
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Desk GetData(int id)
        {
            List<Desk> list = GetAllList();
            return list.FirstOrDefault(t => t.ID == id);
        }

        /// <summary>
        /// 是否存在该餐桌号
        /// </summary>
        /// <param name="Type_Code">类型号</param>
        /// <returns>true/false</returns>
        public static bool IsExist(Desk model)
        {
            bool reStr = false;
            int total = 0;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                total = dbContext.Desks.Where(m => m.DeskNumber.Equals(model.DeskNumber) && m.Status == 1).Count();
                reStr = total == 0 ? false : true;
            }
            return reStr;
        }

        /// <summary>
        /// 删除餐桌
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static ReturnStatus DeleteDesk(string ids)
        {
            ReturnStatus rs;
            try
            {
                using (var dbContext = new EntitiesModel())
                {
                    string[] idList = ids.Split(',');
                    for (int i = 0; i < idList.Length; i++)
                    {
                        int id = StringExtension.GetInt(idList[i], 0);
                        Desk model = dbContext.Desks.FirstOrDefault(o => o.ID == id);
                        dbContext.Delete(model);
                    }
                    dbContext.SaveChanges();
                }
                rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptDeleteSuccess);
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptDeleteFail);
            }
            return rs;
        }

        public static void UpdateDeskStatus(int ID, int status)
        {
            using (var dbContext = new EntitiesModel())
            {
                Desk model = dbContext.Desks.FirstOrDefault(o => o.ID == ID);
                model.Status = status;
                dbContext.SaveChanges();
            }
        }
    }

    /// <summary>
    /// 餐桌状态
    /// </summary>
    public enum DeskStatusEnum
    {
        /// <summary>
        /// 空位
        /// </summary>
        Empty = 1,
        /// <summary>
        /// 用餐中
        /// </summary>
        Dining = 2,
        /// <summary>
        /// 预订
        /// </summary>
        Booked = 3
    }
}
